package com.hjt.gulimall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.Query;
import com.hjt.gulimall.order.dao.OrderReturnReasonDao;
import com.hjt.gulimall.order.entity.OrderEntity;
import com.hjt.gulimall.order.entity.OrderReturnReasonEntity;
import com.hjt.gulimall.order.service.OrderReturnReasonService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;


@RabbitListener(queues = {"spring-queue"})
@Service("orderReturnReasonService")
public class OrderReturnReasonServiceImpl extends ServiceImpl<OrderReturnReasonDao, OrderReturnReasonEntity> implements OrderReturnReasonService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderReturnReasonEntity> page = this.page(
                new Query<OrderReturnReasonEntity>().getPage(params),
                new QueryWrapper<OrderReturnReasonEntity>()
        );

        return new PageUtils(page);
    }


    // 使用@RabbitListener注解，指定监听的队列,
    // Message (详细的信息）
    // 对象T（消息体的具体对象类型）
    // Channel 当前传输数据的通道
//    @RabbitListener(queues = {"spring-queue"})
    @RabbitHandler()
    public void testReceiveMessage(Message message, OrderReturnReasonEntity returnReasonEntity, Channel channel) {
        byte[] body = message.getBody(); // 消息体
        MessageProperties messageProperties = message.getMessageProperties(); // 消息属性
        System.out.println(returnReasonEntity); // 具体的消息类型
        System.out.println(message);


        long deliveryTag = messageProperties.getDeliveryTag(); // channel内自增的一个标签
        try {
            if (deliveryTag % 2 == 0) {
                // 对消息进行确认
                channel.basicAck(deliveryTag, false); // false,非批量签收
            } else {
                // 拒收消息
                // channel.basicReject();
                channel.basicNack(deliveryTag, false, true); // requeue：true--重新入队，false--丢弃
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }




    }
    @RabbitHandler()
    public void testReceiveMessage(OrderEntity orderEntity) {
        System.out.println(orderEntity); // 具体的消息类型
    }
}